package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.itheima.dao.IRoleDao;
import com.itheima.domain.Permission;
import com.itheima.domain.Role;
import com.itheima.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoleServiceImpl implements IRoleService {

    @Autowired
    private IRoleDao roleDao;

    @Override
    public List<Role> findAllRole(Integer pageNum, Integer pageSize) {
        //调用分页查询插件
        PageHelper.startPage(pageNum, pageSize);
        return roleDao.findAllRole();
    }

    /**
     * 添加新角色信息
     *
     * @param role
     */
    @Override
    public void insertRole(Role role) {
        roleDao.insertRole(role);
    }

    /**
     * 根据角色id查询角色详情
     *
     * @param roleId 角色id
     * @return
     */
    @Override
    public Role findByRoleId(String roleId) {
        return roleDao.findByRoleId(roleId);
    }

    /**
     * 根据角色id查询此角色未拥有的权限
     *
     * @param roleId 角色ID
     * @return
     */
    @Override
    public List<Permission> findRolePermission(String roleId) {
        return roleDao.findRolePermission(roleId);
    }

    /**
     * 据角色ID和权限ID，添加此用户未拥有的权限
     *
     * @param roleId      角色ID
     * @param permissions 权限ID
     */
    @Override
    public void insertByRoleIdAndPermissionId(String roleId, String[] permissions) {
        if (permissions!=null&&permissions.length>0){

            for (String permission : permissions) {
                roleDao.insertByRoleIdAndPermissionId(roleId,permission);
            }

        }
    }
}
